package com.shuwang.user.controller;

import com.shuwang.user.domain.UserAdressRequest;
import com.shuwang.user.exception.ConflictException;
import com.shuwang.user.exception.ForbiddenException;
import com.shuwang.user.exception.NotFoundException;
import com.shuwang.user.mapper.UserAddressMapper;
import com.shuwang.user.mapper.UserSigninMapper;
import com.shuwang.user.model.UserAddress;
import com.shuwang.user.model.UserAddressExample;
import com.shuwang.user.until.IDBuilder;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Administrator on 2017/6/5 0005.
 */

@RestController
@RequestMapping(value = "useraddress")
@Api(value = "user_address",description = "用户地址管理API")
public class UserAddressController {

    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserSigninMapper userSigninMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @RequestMapping(value = "adress",method = RequestMethod.POST)
    @ApiOperation(value = "用户新增地址")
    public UserAddress createUserAdress(@ApiParam(value = "请输入用户地址实体类") @RequestBody UserAdressRequest request){

        if (userSigninMapper.selectByPrimaryKey(request.getUserId()) == null){
            log.debug("用户不存在");
            throw new NotFoundException("用户不存在");
        }

        Long addressId = IDBuilder.CreateAnyId("205");
        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(request,userAddress);
        userAddress.setAddressId(addressId);
        userAddressMapper.insertSelective(userAddress);

        UserAddress success = userAddressMapper.selectByPrimaryKey(userAddress.getAddressId());
        if (success == null){
            log.debug("地址保存失败");
            throw new NotFoundException("地址保存失败");
        }

        return success;
    }

    @RequestMapping(value = "adress/userId/{userId}",method = RequestMethod.GET)
    @ApiOperation(value = "根据userId查询用户地址")
    public List<UserAddress> viewUserAdressByUserId(@ApiParam(value = "请输入用户地址实体类") @PathVariable Long userId){

        if (userSigninMapper.selectByPrimaryKey(userId) == null){
            log.debug("用户不存在");
            throw new NotFoundException("用户不存在");
        }

        UserAddressExample example = new UserAddressExample();
        example.createCriteria().andUserIdEqualTo(userId);
        List<UserAddress> userAddresses = userAddressMapper.selectByExample(example);
        if (userAddresses.isEmpty()){
            log.debug("地址不存在");
            throw new NotFoundException("地址不存在");
        }

        return userAddresses;
    }

    @RequestMapping(value = "adress/addressId/{addressId}",method = RequestMethod.GET)
    @ApiOperation(value = "根据adressId查询用户地址")
    public UserAddress viewUserAdressByAddressId(@ApiParam(value = "请输入用户地址实体类") @PathVariable Long addressId){

        UserAddress userAddress = userAddressMapper.selectByPrimaryKey(addressId);
        if (userAddress == null){
            log.debug("地址不存在");
            throw new NotFoundException("地址不存在");
        }

        return userAddress;
    }

    @RequestMapping(value = "adress/update/{addressId}",method = {RequestMethod.POST,RequestMethod.PUT})
    @ApiOperation(value = "根据请输入addressId修改用户地址")
    public UserAddress updateUserAdressByAdressId(@ApiParam(value = "请输入addressId") @PathVariable Long addressId,
                                                  @ApiParam(value = "请输入用户地址实体类") @RequestBody UserAdressRequest request){

        UserAddress userAddress = userAddressMapper.selectByPrimaryKey(addressId);
        if (userAddress == null){
            log.debug("地址不存在");
            throw new NotFoundException("地址不存在");
        }

        if (userSigninMapper.selectByPrimaryKey(request.getUserId()) == null){
            log.debug("用户不存在");
            throw new NotFoundException("用户不存在");
        }

        UserAddress success = new UserAddress();
        BeanUtils.copyProperties(request,success);
        success.setAddressId(userAddress.getUserId());

        if (success.equals(userAddress)){
            log.debug("地址修改失败");
            throw new ForbiddenException("地址修改失败");
        }

        userAddressMapper.updateByPrimaryKey(success);
        return success;
    }

    @RequestMapping(value = "adress/delete/{addressId}",method = RequestMethod.DELETE)
    @ApiOperation(value = "根据adressId删除用户地址")
    public UserAddress deleteUserAdressByAddressId(@ApiParam(value = "请输入用户地址实体类") @PathVariable Long addressId){

        UserAddress userAddress = userAddressMapper.selectByPrimaryKey(addressId);
        if (userAddress == null){
            log.debug("地址不存在");
            throw new NotFoundException("地址不存在");
        }

        userAddressMapper.deleteByPrimaryKey(addressId);
        if (userAddressMapper.selectByPrimaryKey(addressId) != null){
            log.debug("地址删除失败");
            throw new ConflictException("地址删除失败");
        }
        return userAddress;
    }
}
